package com.ruoyi.combat.util.weibo;


import cn.hutool.http.HttpUtil;
import okhttp3.*;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class WeiboOAuth {
    private static final String CLIENT_ID = "594823737";
    private static final String CLIENT_SECRET = "1344aeb423f5fa81fef154c1262c86eb";
    private static final String REDIRECT_URI = "http://15003814330.gnway.cc:80/weibo/getTocken";

    public static String getAccessToken(String code) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("client_id", CLIENT_ID)
                .add("client_secret", CLIENT_SECRET)
                .add("grant_type", "authorization_code")
                .add("code", code)
                .add("redirect_uri", REDIRECT_URI)
                .build();

        Request request = new Request.Builder()
                .url("https://api.weibo.com/oauth2/access_token")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();

        String location = response.header("location");

        System.out.println(location);


        return response.body().string();
    }


    public static void main(String[] args) {
        String url = "https://api.weibo.com/oauth2/authorize?client_id=3365172492&redirect_uri=http://15003814330.gnway.cc:80/weibo/getTocken&response_type=code";

        String s = HttpUtil.get(url);
        System.out.println(s);


    }






    public static void main111(String[] args) throws IOException {

        String originalUrl = "https://api.weibo.com/oauth2/authorize?client_id=594823737&redirect_uri=http://15003814330.gnway.cc:80/weibo/getTocken&response_type=code";
        String redirectUrl = getRedirectUrl(originalUrl);
        System.out.println(redirectUrl);
    }

    public static String getRedirectUrl(String originalUrl) throws IOException {
        String re = "";
        OkHttpClient client = new OkHttpClient.Builder()
                .followRedirects(false) // 禁用自动重定向
                .build();

        Request request = new Request.Builder()
                .url(originalUrl)
//                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
//                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
//                .header("Accept-Encoding", "gzip, deflate, br, zstd")
//                .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
                .header("Cookie", "SINAGLOBAL=7830377756270.393.1739771872527; _s_tentry=www.baidu.com; Apache=2417493437631.206.1739949861669; ULV=1739949861671:3:3:3:2417493437631.206.1739949861669:1739928764381; ALF=1842545110; SUB=_2A25KseOGDeRhGeNO4lQZ9SbEzziIHXVpz3lOrDV8PUJbkNANLVjikW1NTtC49QnzUC59ITDvjkY2E7mYmPtvoJI9; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5GUhsQT_Tuzp3_BhKW0jdB5JpX5KzhUgL.Fo-71KqRSKnRShB2dJLoIpjLxK-L1K2LB.eLxKqLB.2L1K5LxKqL12zL1-zt; UOR=,,www.baidu.com; JSESSIONID=34734DBE1E478718673D39E4D0546141")
//                .header("Sec-Fetch-Dest", "document")
//                .header("Sec-Fetch-Mode", "navigate")
//                .header("Sec-Fetch-Site", "none")
//                .header("Sec-Fetch-User", "?1")
//                .header("Upgrade-Insecure-Requests", "1")
                .build();

        Response response = client.newCall(request).execute();
        if (response.code() == 302) {
            re = response.header("Location"); // 获取重定向的目标 URL
            System.out.println("Redirect to: " + re);
        }
        return re; // 无重定向则返回原地址
    }



}